<?php

namespace app\admin\controller;

use think\Request;
use app\admin\model\AuthGroup as AuthGroupModel;
use app\admin\model\AuthRule as AuthRuleModel;

class AuthGroup extends Base
{

    private $authGroup;
    private $authAuth;

    function initialize()
    {
        parent::initialize();
        $this->authGroup = new AuthGroupModel();
        $this->authAuth = new AuthRuleModel();
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        // 所有用户组
        $groups = $this->authGroup->groupTree();
        // 查询所有的权限
        $rules = $this->authAuth->authRuleTree();

        $this->assign(['rules' => $rules, 'groups' => $groups]);
        return view();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        // 接收数据
        $param = input('post.');

        // 处理数据
        $param['rules'] = empty($param['rules']) ? '' : implode(',', $param['rules']);

        // 父级的level
        $plevel = AuthGroupModel::where('id', $param['pid'])->value('level');
        if ($plevel !== null) {
            $param['level'] = $plevel + 1;
        }
        // 添加数据
        $result = $this->authGroup->save($param);
        if ($result) {
            return redirect('/auth_group');
        }
        $this->error('添加用户组失败!');
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        // 当前用户组
        $group = AuthGroupModel::get($id);
        // 父级的用户组名
        $group['ptitle'] = AuthGroupModel::where('id', $group['pid'])->value('title');

        return $group;
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        // 当前用户组
        $group = AuthGroupModel::get($id);
        // 父级的用户组名
        $group['ptitle'] = AuthGroupModel::where('id', $group['pid'])->value('title');

        return $group;

    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        // 接收数据
        $param = input('put.');
        unset($param['_method']);

        // 处理数据
        $param['rules'] = empty($param['rules']) ? '' : implode(',', $param['rules']);
        // 父级的level
        $plevel = AuthGroupModel::where('id', $param['pid'])->value('level');
        if ($plevel !== null) {
            $param['level'] = $plevel + 1;
        }

        $result = $this->authGroup->save($param, ['id' => $id]);
        if ($result) {
            return redirect('/auth_group');
        }
        $this->error('更新用户组失败!');
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        // 所有的子用户组 ID
        $sonAuthGroupIDs = $this->authGroup->recursiveGetSonIDs($id);

        // 删除所有子用户组下所有用户
        foreach ($sonAuthGroupIDs as $k => $v) {
            $this->authGroup->delSameGroupUsers($v);
        }

        // 所有要删除的用户组ID
        $delAuthGroupIDs = $sonAuthGroupIDs;
        $delAuthGroupIDs[] = $id;

        // 删除当前组
        $result = AuthGroupModel::destroy($delAuthGroupIDs);
        if ($result) {
            return 1;
        }
        return 0;

    }

    /**
     * 是否有子用户组
     */
    function getRules() {
        $id = input('id');
        echo $this->authGroup->getRules($id);
    }

}
